package com.example.administrator.personalmail.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.administrator.personalmail.Activity.CreateOrderActivity;
import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.model.ShoppingCart;
import com.example.administrator.personalmail.model.Wares;
import com.example.administrator.personalmail.widget.CartProvider;
import com.example.administrator.personalmail.widget.NumberAddSubView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

public class CarAdapter extends RecyclerView.Adapter {
    private ArrayList<ShoppingCart> mDatas;
    private CheckBox mCheckBox_all;
    private TextView tvTotalPrice;
    private CartProvider cartProvider;
    private Button btEdit;
    private Button btOrder;
    private Button btDel;

    public CarAdapter(final Context context, List<ShoppingCart> mDatas, final CheckBox checkBox, TextView textView, Button button, Button order, Button del) {
        this.mDatas = (ArrayList<ShoppingCart>) mDatas;
        btOrder = order;
        btDel = del;
        btDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                delItems();
            }
        });
        btEdit = button;
        btEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (btEdit.getText().equals("编辑")){
                    btEdit.setText("完成");
                    checkAll(false);
                    mCheckBox_all.setChecked(false);
                    tvTotalPrice.setVisibility(View.GONE);
                    btOrder.setVisibility(View.GONE);
                    btDel.setVisibility(View.VISIBLE);
                }else {
                    btEdit.setText("编辑");
                    checkAll(true);
                    mCheckBox_all.setChecked(true);
                    tvTotalPrice.setVisibility(View.VISIBLE);
                    btOrder.setVisibility(View.VISIBLE);
                    btDel.setVisibility(View.GONE);
                }


            }
        });
        mCheckBox_all = checkBox;
        mCheckBox_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAll(mCheckBox_all.isChecked());
            }
        });
        tvTotalPrice = textView;
        cartProvider = new CartProvider(context);
        showTotalPrice();
        checkBoxAll();
    }

    private void checkAll(boolean checked) {
        if (mDatas != null && mDatas.size()>0){
            for (ShoppingCart shoppingCart : mDatas){
                shoppingCart.setIsChecked(checked);
                notifyDataSetChanged();
            }
        }
        showTotalPrice();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.template_cart,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        ShoppingCart shoppingCart = mDatas.get(position);

        viewHolder.tvTitle.setText(shoppingCart.getName());
        viewHolder.tvPrice.setText("￥ "+shoppingCart.getPrice());
        viewHolder.fvCar.setImageURI(Uri.parse(shoppingCart.getImgUrl()));
        viewHolder.checkBox.setChecked(shoppingCart.isChecked());
        viewHolder.numberAddSubView.setValue(shoppingCart.getCount());

    }

    @Override
    public int getItemCount() {
        if (mDatas!=null){
            return mDatas.size();
        }
        return 0;
    }

    private void showTotalPrice(){
        float total = getTotalPrice();
        tvTotalPrice.setText("合计: " + total+" 元");
    }

    public float getTotalPrice(){
        float sum=0;
        if (mDatas != null && mDatas.size()>0){
            for (ShoppingCart shoppingCart : mDatas){
                if (shoppingCart.isChecked()){
                    sum  += shoppingCart.getCount() * shoppingCart.getPrice();
                }
            }
            return sum;
        }
        return sum;
    }

    private void checkBoxAll(){
        int count = 0;
        int num = 0;
        if (mDatas != null && mDatas.size()>0){
            num = mDatas.size();
            for (ShoppingCart shoppingCart : mDatas){
                if (!shoppingCart.isChecked()){
                    mCheckBox_all.setChecked(false);
                    return;
                }else {
                    count++;
                }
            }
            if (count == num){
                mCheckBox_all.setChecked(true);
            }
        }else {
            mCheckBox_all.setChecked(false);
        }

    }

    private void delItems(){
        int id = 0;
        List<ShoppingCart> temp = new ArrayList<>();
        if (mDatas != null && mDatas.size()>0){
            for (ShoppingCart shoppingCart : mDatas){
                if (shoppingCart.isChecked()){
                    cartProvider.delete(shoppingCart);
                }else {
                    temp.add(shoppingCart);
                }
                id++;
            }
            mDatas.clear();
            mDatas.addAll(0,temp);
            notifyDataSetChanged();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private CheckBox checkBox;
        private TextView tvTitle;
        private TextView tvPrice;
        private SimpleDraweeView fvCar;
        private NumberAddSubView numberAddSubView;

        public ViewHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.checkbox_car);
            tvTitle = itemView.findViewById(R.id.tv_title_car);
            tvPrice = itemView.findViewById(R.id.tv_price_car);
            fvCar = itemView.findViewById(R.id.fv_car);
            numberAddSubView = itemView.findViewById(R.id.num_control_car);

            numberAddSubView.setOnButtonClickListener(new NumberAddSubView.OnButtonClickListener() {
                @Override
                public void onButtonAddClick(View view, int value) {
                    ShoppingCart shoppingCart = mDatas.get(getLayoutPosition());
                    shoppingCart.setCount(value);
                    cartProvider.update(shoppingCart);
                    showTotalPrice();
                }

                @Override
                public void onButtonSubClick(View view, int value) {
                    ShoppingCart shoppingCart = mDatas.get(getLayoutPosition());
                    shoppingCart.setCount(value);
                    cartProvider.update(shoppingCart);
                    showTotalPrice();
                }
            });

            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShoppingCart shoppingCart = mDatas.get(getLayoutPosition());
                    shoppingCart.setIsChecked(!shoppingCart.isChecked());

//                    cartProvider.update(shoppingCart);
                    checkBoxAll();
                    showTotalPrice();
                }
            });
        }
    }
}
